package logic

import (
	"context"
	"hotel/hotelrpc/hotelrpc"
	"hotel/pkg"
	"strconv"

	"hotel/hotelapi/internal/svc"
	"hotel/hotelapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginHotelLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginHotelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginHotelLogic {
	return &LoginHotelLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginHotelLogic) LoginHotel(req *types.LoginHotelRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	hotel, err := l.svcCtx.HotelRpc.LoginHotel(l.ctx, &hotelrpc.LoginHotelRequest{
		Username: req.Usrname,
		Password: req.Password,
		Code:     req.Code,
	})
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: pkg.Tri(err.Error()),
			Data:    nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(hotel.Id, 10))
	if err != nil {
		return &types.Response{
			Code:    500,
			Message: pkg.Tri(err.Error()),
			Data:    nil,
		}, nil
	}
	return &types.Response{
		Code:    200,
		Message: "登录成功",
		Data:    handler,
	}, nil
}
